package com.lwj.controller;

import com.lwj.common.util.PageResult;
import com.lwj.common.util.R;
import com.lwj.entity.Flower;
import com.lwj.service.FlowerService;
import com.lwj.vo.FlowerVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;


@RestController
@RequestMapping("/flower")
public class FlowerController {
	@Autowired
	private FlowerService flowerService;

	@RequestMapping("/list")
	public R getFlower() {
		return R.ok().data("list",flowerService.list());
	}

	@GetMapping("/findByPage/{page}/{pageSize}")
	public R findByPage(@PathVariable("page") Integer page,
	                    @PathVariable("pageSize") Integer pageSize) {
		PageResult<Flower> pr = flowerService.findByPage(page,pageSize);
		return R.ok().data("pr",pr);
	}

	@PostMapping("/search/{page}/{pageSize}")
	public R search(@PathVariable("page") Integer page,
	                @PathVariable("pageSize") Integer pageSize,
	                @RequestBody(required = false) FlowerVO vo){
		PageResult<Flower> pr = flowerService.search(page,pageSize,vo);
		return R.ok().data("pr",pr);
	}


	@PostMapping("/add")
	public R add(@RequestBody Flower flower){
		boolean flag = flowerService.add(flower);
		return flag ? R.ok().message("耶！添加花朵成功啦!(＾－＾)V") : R.error().message("添加花朵失败o(╥﹏╥)o!");
	}


	@PutMapping("/update")
	public R update(@RequestBody Flower flower){
		boolean flag = flowerService.updateById(flower);
		return flag ? R.ok().message("修改花朵成功啦(＾－＾)V!") : R.error().message("修改花朵失败o(╥﹏╥)o!");
	}

	@DeleteMapping("/delete/{ids}")
	public R delete(@PathVariable("ids") String ids){
		String[] split = ids.split(",");
		List<String> sids = Arrays.asList(split);
		return flowerService.removeByIds(sids) ? R.ok().message("耶！删除花朵成功啦!(＾－＾)V") : R.error().message("删除花朵失败o(╥﹏╥)o!");
	}
}
